﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model.Entities
{
    public class Module
    {
        private object _settings = null;

        public int ID { get; set; }

        public string Name { get; set; }

        public string NamespaceController { get; set; }

        public string ContentTypeHelper { get; set; }

        public string Views { get; set; }

        public string Description { get; set; }

        public string Position { get; set; }

        public string Order { get; set; }

        //Get Setting attribute by key input
        public string this[string key]
        {
            get
            {
                if (Settings != null)
                {
                    string oKey = "<" + key + ">", cKey = "</" + key + ">";
                    string str = ((List<string>)DefaultSettings).Where(x => x.Contains(cKey)).FirstOrDefault();
                    return str.Substring(oKey.Length, str.Length - (cKey.Length + oKey.Length));
                }
                return null;
            }
        }

        public string Settings
        {
            get
            {
                string strReturn = string.Empty;
                if (((List<string>)DefaultSettings) != null && ((List<string>)DefaultSettings).Count > 0)
                {
                    foreach (var item in ((List<string>)DefaultSettings))
                    {
                        strReturn += item.ToString();
                    }
                }
                return strReturn;
            }
            
        }

        public object DefaultSettings
        {
            get
            {
                if (_settings != null && _settings.GetType().Equals(typeof(XmlNode[])))
                {
                    return ((XmlNode[])_settings).Select(x => x.OuterXml).ToList<string>();
                }
                return null;
            }
            set { _settings = value; }
        }

        public virtual ICollection<PageModuleSettings> PageModuleSettings { get; set; }
    }
}
